﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;
using Eneta.Portal.Common.Helpers;
using Microsoft.SharePoint.Publishing;

namespace Eneta.Portal.Literature.Controls
{
    public class LiteratureCommentsViewControl : UserControl
    {
        protected Repeater CommentsRepeater;
        protected HtmlGenericControl CommentsCount;

        public string WebUrl { get; set; }
        public string ListName { get; set; }

        public int ItemId { get; set; }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.CommentsRepeater.ItemDataBound += new RepeaterItemEventHandler(CommentsRepeater_ItemDataBound);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (IsPostBack)
                return;

            BindComments();
        }

        private void BindComments()
        {
            if (ItemId == 0)
                return;
            var currentWeb = SPContext.Current.Web;
            var repository = new LiteratureRepository(currentWeb);
            var currentItem = repository.GetLiteratureItemById(ItemId);
            var itemListInfo = currentWeb.ServerRelativeUrl + ";" + PublishingWeb.GetPagesListName(currentWeb);
            var comments = repository.GetLiteratureComments(WebUrl, ListName, itemListInfo, currentItem.ID);

            CommentsCount.InnerText = comments.Count.ToString();
            CommentsRepeater.DataSource = comments;
            CommentsRepeater.DataBind();
        }



        void CommentsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem == null)
                return;

            var comment = (LiteratureCommentDto)e.Item.DataItem;
            var commentProfileLink = (HtmlAnchor)e.Item.FindControl("CommentProfileLink");
            var commentProfileImage = (HtmlImage)e.Item.FindControl("CommentProfileImage");
            var commentBody = (HtmlGenericControl)e.Item.FindControl("CommentBody");
            var commentProfileNameLink = (HtmlAnchor)e.Item.FindControl("CommentProfileNameLink");
            var commentTime = (HtmlGenericControl)e.Item.FindControl("CommentTime");

            commentProfileLink.HRef = UrlHelper.GetProfileUrl(comment.PosterProfileId);
            commentProfileLink.Title = comment.PosterName;
            commentProfileImage.Src = UrlHelper.GetUserImageHandlerUrl(32, comment.PosterProfileId);
            commentProfileImage.Alt = comment.PosterName;

            commentBody.InnerHtml = comment.Comment.Replace("\n", "<br/>");
            commentProfileNameLink.HRef = commentProfileLink.HRef;
            commentProfileNameLink.Title = commentProfileLink.Title;
            commentProfileNameLink.InnerText = commentProfileLink.Title;
            commentTime.InnerText = comment.Created.ToString();
        }
    }
}
